#ifndef _SLIDER_H
#define _SLIDER_H

#include "GraphicElement.h"

const float SL_SPACE = 4.5;
const int SL_CUBE = 9;

class Slider: public GraphicElement
{
private:
	float progress; // 0 - 100

	//bool updated;
	bool is_mouse_down;
	int lastMousePoint1, lastMousePoint2;
	int control_id;

	Bitmap *sliderHeadBg;

public:
	Slider(int x, int y, int width, int height, HWND hWnd, int control_id, WCHAR imgPath[], int imgX, int imgY): GraphicElement(x, y, width, height, hWnd)
	{
		this->progress = 0;
		is_mouse_down = false;
		//updated = false;
		this->control_id = control_id;




		// Randa exe kelia
		WCHAR buffer[MAX_PATH];
		GetModuleFileNameW(NULL, buffer, sizeof(buffer));
		wstring exeDir = buffer;
		int place = exeDir.find_last_of('\\');
		exeDir = exeDir.substr(0, place);
		_wchdir(exeDir.c_str());

		//wstring imPat = imgPath;
		int counter = 0;
		for(int i = place+1; i < MAX_PATH; i++)
		{
			buffer[i] = imgPath[counter];
			if(imgPath[counter] == '\0')
			{
				break;
			}
			counter++;
		}

		sliderHeadBg = new Bitmap(width, height);
		Bitmap *b = new Bitmap(buffer); //L"img\\bitmap.bmp"
		Graphics *fromImgButtonBg = Graphics::FromImage(sliderHeadBg);
		fromImgButtonBg->DrawImage(b, 0, 0, imgX, imgY, 9, 7, UnitPixel);

		delete b;
		delete fromImgButtonBg;

	}

	~Slider()
	{
		delete sliderHeadBg;
	}



	void Draw(HDC hdc)
	{
	
		if(redraw)
		{
		
			int lineH = 2;

			int center = (int)((dimensions->width-(SL_SPACE*2)) * ((float)progress/100));

			Bitmap bmp(dimensions->width,dimensions->height);
			Graphics *fromImg = Graphics::FromImage(&bmp);


			//SolidBrush  brush(Color(255, 0, 0, 255));
		
		

			//SolidBrush  progr(Color(255, 0, 162, 232));
			//SolidBrush  left(Color(255, 195, 195, 195));

	
			SolidBrush  brushSlider(SLIDER_HEAD_COLOR);
			SolidBrush  brushBackground(SLIDER_BACKGROUND_COLOR);
			Pen      penBorder(SLIDER_BORDER_COLOR);
			Pen      penLine(SLIDER_CENTER_LINE_COLOR);


			//fromImg->DrawRectangle(&penBorder, 0, 0, dimensions->width-1, dimensions->height-1);
			fromImg->FillRectangle(&brushBackground, 0, 0, dimensions->width, dimensions->height);


			fromImg->DrawLine(&penLine, 0, (int)((dimensions->height / 2) +0.5), dimensions->width-1, (int)((dimensions->height / 2) +0.5));


	
			//fromImg->FillRectangle(&progr, 1, height/2-(lineH/2) , center, lineH);
			//fromImg->FillRectangle(&left, center, height/2-(lineH/2) , width-2, lineH);

			/*fromImg->FillRectangle(&progr, 1 + SL_SPACE, height/2-(lineH/2) , center, lineH);
			fromImg->FillRectangle(&left, center+1+ SL_SPACE, height/2-(lineH/2) , (width-center)-2-2*SL_SPACE, lineH);*/

			//int c_cube = (int)((width) * ((float)progress/100)) - SL_CUBE/2;
			int c_cube = center - (int)((float)SL_CUBE/2+0.5) + SL_SPACE;

			//c_cube = 

			//fromImg->FillRectangle(&brushSlider, c_cube, 1, SL_CUBE, dimensions->height-2);
			//fromImg->DrawRectangle(&penBorder, c_cube, 0, SL_CUBE-1, dimensions->height-1);


			fromImg->DrawImage(sliderHeadBg, center, (int)((dimensions->height / 2) +0.5)-3);

	
			Graphics    graphics(hdc);
			graphics.DrawImage(&bmp, dimensions->x, dimensions->y, dimensions->width, dimensions->height);

			delete fromImg;
			redraw = false;
		}
		if(is_mouse_down)
			{
				calculate();
			}
	}

	float setProgress(float p)
	{
		float r = -1;
		if(!is_mouse_down || lastMousePoint1 == lastMousePoint2)
		{
			if(p < 0) p = 0.0;
			if(p > 100) p = 100.0;
			progress = p;
		}
		else
		{
			r = progress;
		}

		UpdateGraphics();
		return r;
	}

	float getProgress()
	{
		return progress;
	}

	


	void MouseDown()
	{
	
		if(checkBoundingBox())
		{
			is_mouse_down = true;
		

			progress = calculate();
		
			lastMousePoint1 = 0;
			lastMousePoint2 = 1;
		}
	}



	void MouseUp()
	{
		is_mouse_down = false;
	}

	int calculate()
	{
		LPPOINT p = new POINT;
		GetCursorPos(p);
		ScreenToClient(hWnd, p);
	
		int tmp = p->x - dimensions->x - SL_SPACE;
		lastMousePoint1 = lastMousePoint2;
		lastMousePoint2 = tmp;


		if(p->x >= dimensions->x+1+SL_SPACE && p->x <= dimensions->x + dimensions->width-1-SL_SPACE)
		{
		
			if(lastMousePoint1 != tmp) progress = (float)tmp/(dimensions->width-2*SL_SPACE)*100;
		}	
		else
		{
		
			if(p->x < dimensions->x+1 + SL_SPACE) progress = 0.01;
			if(p->x > dimensions->x + dimensions->width-1 - SL_SPACE) progress = 100;
		
		}

	

		delete[] p;

		if(lastMousePoint1 != lastMousePoint2) 
		{ 
			SendMessage(hWnd, WM_COMMAND, control_id, 0);
			UpdateGraphics();
		}
		return 0;
	}



};

#endif